<?php
/**
 * @category     APPCore
 * @package     APPCore >> Mapper
 * @copyright     Copyright(c) 2000-2013
 * @license     http://www.yourdomain.com
 * @version     APPCore version 1.0.0
 * @author         
 * @implement     Name of developer
 */


class Model_Entities_Mapper_AclResourceMapper
{

    /**
     * @var Model_Entities_DbTable_AclResource
     */
    protected $_dbTable = null;

    /**
     * Get registered Zend_Db_Table instance
     * 
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if ($this->_dbTable === null) {
            $this->setDbTable('Model_Entities_DbTable_AclResource');
        }
        return $this->_dbTable;
    }

    /**
     * Specify Zend_Db_Table instance to use for data operations
     * 
     * @param Zend_Db_Table_Abstract $dbTable
     * @return Model_Entities_Mapper_AclResourceMapper
     */
    public function setDbTable($dbTable = null)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }if (! $dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Zend_Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Save a AclResource entry
     * 
     * @param Model_Entities_AclResource $object
     * @return void
     */
    public function save(Model_Entities_AclResource $object, 
    Array $filterArray = array(), $ignoreEmptyValuesOnUpdate = true, $pkey = 'id')
    {
        $data = array(
            'id' => $object->getId(),
            'resource' => $object->getResource()
        );
        if ($ignoreEmptyValuesOnUpdate) {
            foreach ($data as $key => $value)
                if (is_null($value) or $value == '')
                unset($data [$key]);
        }
        if (null ===($id = $object->getId())) {
            unset($data ['id']);
            $this->getDbTable()->insert($data);
            return $this->getDbTable()->getDefaultAdapter()->lastInsertId();
        } else {
            if ($this->getDbTable()->update($data, array('id = ?' => $id)) < 0) {
                return false;
            } else {
                return $id;
            }
        }
    }

    /**
     * Get data array
     * 
     * @param Model_Entities_AclResource $object
     * @return array
     */
    public function toArray(Model_Entities_AclResource $object)
    {
        return array(
            'id' => $object->getId(),
            'resource' => $object->getResource()
        );
    }

    /**
     * Find an entry by id
     * 
     * @param Model_Entities_AclResource $object
     * @return array
     */
    public function find($key, Model_Entities_AclResource $object)
    {
        $result = $this->getDbTable()->find($key);
        if (0 == count($result)) {
            return null;
        }
        $dtr = $result->current();
        $dtr = $dtr->toArray();
        $object->setId($dtr['id'])
            ->setResource($dtr['resource'])
            ->setMapper($this);
    }

    /**
     * Find an entry by id
     * 
     * @param Model_Entities_AclResource $object
     * @return array
     */
    public function search($column, $keyword)
    {
        $select = $this->getDbTable()->select();
        $select->where("$column = ?", $keyword);
        $dts = $this->getDbTable()->fetchAll($select);
        $entries = array();
        foreach ($dts as $key => $dtr) {
            $dtr = $dtr->toArray();
            $object = new Model_Entities_AclResource();
            $object->setId($dtr['id'])
                ->setResource($dtr['resource'])
                ->setMapper($this);
            $entries [$key] = $object;
        }
        return $entries;
    }

    /**
     * Delete entry(ies) by id(s)
     * 
     * @param array $ids
     * @return mixed
     */
    public function delete(Array $ids)
    {
        if (! is_array($ids)) {
            $ids = array($ids);
        }
        $wheres = 'id IN(' . implode(',', $ids) . ')';
        return $this->getDbTable()->delete($wheres);
    }

    /**
     * Fetch all data entries
     * 
     * @param Zend_Db_Table_Select $select
     * @return mixed
     */
    public function fetchAll()
    {
        $dts = $this->getDbTable()->fetchAll();
        return $dts->toArray();
    }

    /**
     * Fetch all data entries
     * 
     * @param int $page
     * @param int $limit
     * @param Array $wheres
     * @param Array $orders
     * @return Array
     */
    public function getArray($page, $limit, Array $wheres = array(), Array $orders = array())
    {
        $select = $this->getDbTable()->select();
        if (is_array($wheres) and count($wheres) > 0) {
            foreach ($wheres as $cond => $value) {
                $select->where($cond, $value);
            }
        }
        if (is_array($orders) and count($orders) > 0) {
            foreach ($orders as $order) {
            $select->order($order);
            }
        }
        if ($limit > 0) {
            $select->limitPage($page, $limit);
        }
        $select->distinct(true);
        return $this->getDbTable()->fetchAll($select)->toArray();
    }

    /**
     * Fetch all data entries
     * 
     * @param int $page
     * @param int $limit
     * @param Array $wheres
     * @param Array $orders
     * @return Array
     */
    public function getObject($page, $limit, Array $wheres = array(), Array $orders = array())
    {
        $select = $this->getDbTable()->select();
        if (is_array($wheres) and count($wheres) > 0) {
            foreach ($wheres as $cond => $value) {
                $select->where($cond, $value);
            }
        }
        
        if (is_array($orders) and count($orders) > 0) {
            foreach ($orders as $order) {
            $select->order($order);
            }
        }
        if ($limit > 0) {
            $select->limitPage($page, $limit);
        }
        $select->distinct(true);
        return $this->fetchAll($select);
    }

    /**
     * Get total entries
     * 
     * @param Array $wheres
     * @return int
     */
    public function getTotalEntries(Array $wheres = array())
    {
        $array = array('sm'=>$this->getDbTable()->getTableName());
        $select = $this->getDbTable()->select()->from($array, array('num'=>'count(*)'));
        if (is_array($wheres) and count($wheres) > 0) {
            foreach ($wheres as $cond => $value) {
                $select->where($cond, $value);
            }
        }
        return $this->getDbTable()->fetchRow($select)->num;
    }


}

